package com.ica.model.policy;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.util.Log;

import com.ica.model.command.AbstractCommand;
import com.ica.model.contact.ContactGroup;
import com.ica.model.policy.time.TimeStamp;
import com.ica.model.policy.time.TimeTableManager;
import com.ica.model.policy.time.utils.TimeUtils.DAYS;
import com.ica.model.util.IdentifiableModelObject;
import java.util.Collection;

/**
 * @author   Hector
 */
public class Policy extends IdentifiableModelObject {
	
	/**
	 */
	private String name = null;
	/**
	 * @uml.property  name="timeTableManager"
	 * @uml.associationEnd  
	 */
	private TimeTableManager timeTableManager = null;
	/**
	 * @uml.property  name="command"
	 * @uml.associationEnd  
	 */
	private AbstractCommand command = null;
	/**
	 */
	private List<ContactGroup> contacts = null;
	
	public Policy(String name) {		
		this.setName(name);
		timeTableManager = new TimeTableManager();
	}

	/**
	 * @return
	 * @uml.property  name="name"
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param  name
	 * @uml.property  name="name"
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return
	 * @uml.property  name="command"
	 */
	public AbstractCommand getCommand() {
		return command;
	}

	/**
	 * @param  command
	 * @uml.property  name="command"
	 */
	public void setCommand(AbstractCommand command) {
		this.command = command;
	}
	
	/**
	 * @return
	 * @uml.property  name="contacts"
	 */
	public List<ContactGroup> getContacts() {
		return contacts;
	}

	/**
	 * @param  contacts
	 * @uml.property  name="contacts"
	 */
	public void setContacts(List<ContactGroup> contacts) {
		this.contacts = contacts;
	}

	public void addAllTimeStamps(DAYS day, List<TimeStamp> timeStamp) {		
		timeTableManager.addAllTimeStamp(day, timeStamp);
	}
	
	public void replaceAllTimeStamps(HashMap<DAYS,List<TimeStamp>> timeStamps) {
		timeTableManager.replaceAllTimeStamps(timeStamps);
	}
	
	public List<TimeStamp> getTimeStamps(DAYS day) {		
		return timeTableManager.getTimeStamps(day);
	}
	
	public boolean test(String phoneNumber) {		
		Log.w("DEBUG","Incoming phone call: "+phoneNumber);
		if (contacts==null) return false;
		if (!timeTableManager.contains(Calendar.getInstance())) return false;
		for(ContactGroup contactGroup : contacts)
			if (contactGroup.contains(phoneNumber)) return true;
		return false;
	}
	
	public boolean executeCommand(Context context) {		
		if (command==null) return false;
		return command.execute(context);
	}
	
	@Override
	public String toString() {		
		return name;
	}

	/** 
	 * @uml.property name="contactGroup"
	 * @uml.associationEnd multiplicity="(0 -1)" inverse="policy:com.ica.model.contact.ContactGroup"
	 */
	private Collection<ContactGroup> contactGroup;

	/** 
	 * Getter of the property <tt>contactGroup</tt>
	 * @return  Returns the contactGroup.
	 * @uml.property  name="contactGroup"
	 */
	public Collection<ContactGroup> getContactGroup() {
		return contactGroup;
	}

	/** 
	 * Setter of the property <tt>contactGroup</tt>
	 * @param contactGroup  The contactGroup to set.
	 * @uml.property  name="contactGroup"
	 */
	public void setContactGroup(Collection<ContactGroup> contactGroup) {
		this.contactGroup = contactGroup;
	}
}
